<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\SendEmailRequest;
use App\Mail\Authorized;
use App\Models\SysDistrict;
use App\Models\SysProvince;
use App\Models\SysCity;
use App\Services\EmailService;
use App\Transformers\DistrictTransformer;
use App\Transformers\ProvinceTransformer;
use App\Transformers\CityTransformer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class PublicController extends Controller
{
    public function ProvinceList(Request $request)
    {
        $search = $request->get('search');

        $province_id = $request->get('province_id');

        $provinces = SysProvince::where(function ($query) use ($search, $province_id) {
            if ($search) {
                $query->where('name', 'like', '%' . $search . '%');
            }
            if ($province_id) {
                $query->where('id', $province_id);
            }
        })->get();

        return $this->response->collection($provinces, new ProvinceTransformer());

    }

    public function city(Request $request)
    {
        $search = $request->get('search');

        $province_id = $request->get('province_id');

        $city_id = $request->get('city_id');

        $city = SysCity::where(function ($query) use ($search, $province_id, $city_id) {
            if ($province_id) {
                $query->where('province_id', $province_id);
            }

            if ($search) {
                $query->where('name', 'like', '%' . $search . '%');
                $query->orWhere('full_spell', 'like', '%' . $search . '%');
                $query->orWhere('first_spell', 'like', '%' . $search . '%');
            }
            if ($city_id) {
                $query->where('id', $city_id);
            }
        })
            ->get();

        return $this->response->collection($city, new CityTransformer());

    }

    public function district(Request $request)
    {
        $search = $request->get('search');

        $city_id = $request->get('city_id');

        $district = SysDistrict::where(function ($query) use ($search, $city_id) {
            if ($city_id) {
                $query->where('city_id', $city_id);
            }
            if ($search) {
                $query->where('name', 'like', '%' . $search . '%');
            }
        })->get();

        return $this->response->collection($district, new DistrictTransformer());
    }

    /**
     * 发送验证码邮件
     *
     * @param SendEmailRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function sendVerificationEmail(SendEmailRequest $request)
    {
        $service = new EmailService();

        $service->store($request);

        return response()->json(['message' => trans('api.deliver_succeed'), 'code' => 1, 'status' => true, 'data' => ''], 200);
    }

    /**
     * 验证邮箱验证码
     *
     * @param SendEmailRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ApiException
     */
    public function checkVerificationCode(SendEmailRequest $request)
    {
        $email = $request->get('email');

        $verification_code = $request->get('verification_code', false);

        if (!$verification_code) {
            $this->response->error(trans('api.verification_code_invalid'), 200);
        }

        $service = new EmailService();

        $result = $service->verify($email, $verification_code);

        if ($result) {
            return response()->json(['message' => trans('api.validate_succeed'), 'code' => 1, 'status' => true, 'data' => ''], 200);
        }
    }
}
